package com.chenyun.system.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chenyun.common.annotation.Log;
import com.chenyun.common.annotation.Page;
import com.chenyun.common.enums.operlog.BusinessTypeEnum;
import com.chenyun.system.model.SysPost;
import com.chenyun.system.service.ISysPostService;
import com.chenyun.boot.web.controller.BaseController;
import com.chenyun.common.constants.UserConstants;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/post")
@Api(tags = "岗位信息-admin")
public class SysPostController extends BaseController<ISysPostService, SysPost> {
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @ApiOperation("获取岗位列表")
    @GetMapping("/list")
    @Page
    @SaCheckPermission("system:post:list")
    public PageResult list(SysPost post) {
//        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @ApiOperation("根据岗位编号获取详细信息")
    @GetMapping(value = "/{postId}")
    @SaCheckPermission("system:post:info")
    public AjaxResult getInfo(@PathVariable Long postId) {
        return AjaxResult.SUCCESS(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @ApiOperation("新增岗位")
    @PostMapping
    @Log(title = "新增岗位", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:post:add")
    public AjaxResult add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return AjaxResult.FAIL("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return AjaxResult.FAIL("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
//        post.setCreateBy(SecurityUtils.getUsername());
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @ApiOperation("修改岗位")
    @PostMapping("edit")
    @Log(title = "修改岗位", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:post:edit")
    public AjaxResult edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return AjaxResult.FAIL("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return AjaxResult.FAIL("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
//        post.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @ApiOperation("删除岗位")
    @PostMapping("del")
    @Log(title = "删除岗位", businessType = BusinessTypeEnum.INSERT)
    @SaCheckPermission("system:post:del")
    public AjaxResult remove( Long postIds) {
        return toAjax(postService.deletePostByIds(new Long[]{postIds}));
    }

    /**
     * 获取岗位选择框列表
     */
    @ApiOperation("获取岗位选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return AjaxResult.SUCCESS(posts);
    }
}
